﻿#include "DelimitedTextReader.h"

#include <algorithm>
#include <vtkDelimitedTextReader.h>
#include <vtkTable.h>
#include <vtksys/SystemTools.hxx>
#include <vtkFloatArray.h>
#include "CommonFunction.h"

namespace pst
{

DelimitedTextReader::DelimitedTextReader(QObject* parent)
    : QObject(parent)
    , m_filePath()
    , m_reader(vtkDelimitedTextReader::New())
    , m_shouldAddHeader(false)
    , m_table(nullptr)
    , m_XTitles{}
    , m_YTitles{}
{

}

DelimitedTextReader::~DelimitedTextReader()
{
    m_reader->Delete();
}

void DelimitedTextReader::SetFilePath(const QString &filePath)
{
    m_filePath = filePath;
}

void DelimitedTextReader::SetHaveHeaders(bool haveHeaders)
{
    m_reader->SetHaveHeaders(haveHeaders);
}

void DelimitedTextReader::SetFieldDelimiterCharacters(const QString &flag)
{
    m_reader->SetFieldDelimiterCharacters(flag.toStdString().c_str());
}

void DelimitedTextReader::AddTableHeaderAuto(bool addHeader)
{
    m_shouldAddHeader = addHeader;
}

bool DelimitedTextReader::Update()
{
    if (!vtksys::SystemTools::FileExists(m_filePath.toStdString(), true))
    {
        std::cout << "Read 2D text file failed!" << std::endl;
        std::cout << "File name is "<<m_filePath.toStdString()<<std::endl;
        return false;
    }

    m_reader->SetFileName(m_filePath.toStdString().c_str());
    m_reader->DetectNumericColumnsOn();
    try
    {
        m_reader->Update();
    }
    catch (const std::exception& e )
    {
        std::cout << "Read 2D text file failed!" << std::endl;
        std::cout<<e.what()<<std::endl;
        return false;
    }

    if(m_shouldAddHeader)
    {
        AddHeaderFromOriginalTable();
    }
    else
    {
        m_table = m_reader->GetOutput();
    }

    if(m_table->GetNumberOfColumns()<2||m_table->GetNumberOfRows()<2)
    {
        std::cout<<"Error! The table needs at least two rows and two columns!"<<std::endl;
        return false;
    }

    //解析txt

    GenerateXYTitle();

    return true;
}

vtkTable* DelimitedTextReader::GetOutput()
{
    return m_table;
}

std::vector<std::string> DelimitedTextReader::GetXTitles()
{
    return m_XTitles;
}

std::vector<std::string> DelimitedTextReader::GetYTitles()
{
    return m_YTitles;
}

void DelimitedTextReader::AddHeaderFromOriginalTable()
{
    auto tempTable = m_reader->GetOutput();
    auto numOfColumes = tempTable->GetNumberOfColumns();
    auto numOfRows = tempTable->GetNumberOfRows();

    //添加第一列 x轴
    vtkNew<vtkFloatArray> arrX;
    arrX->SetName("x");
    arrX->SetNumberOfComponents(1);
    arrX->SetNumberOfTuples(numOfRows);
    for(int i = 0; i<numOfRows; ++i)
    {
        arrX->SetValue(i,i);
    }
    m_table->AddColumn(arrX);

    //复制其他列
    for(int i =0;i<numOfColumes;++i)
    {
        auto arrY = tempTable->GetColumn(i);
        arrY->SetName(("y_"+std::to_string(i)).c_str());
        m_table->AddColumn(arrY);
    }
}

void DelimitedTextReader::GenerateXYTitle()
{
    auto XArrayName = CommonFunction::trim(std::string(m_reader->GetOutput()->GetColumn(0)->GetName()));
    auto YArrayNameTotal = m_reader->GetOutput()->GetColumn(1)->GetName();
    std::vector<std::string> tempRes;
    CommonFunction::Stringsplit(YArrayNameTotal, "=", tempRes);

    auto YArrayName = CommonFunction::trim(tempRes[0]); //phi
    transform(XArrayName.begin(), XArrayName.end(), XArrayName.begin(), ::toupper);
    transform(YArrayName.begin(), YArrayName.end(), YArrayName.begin(), ::toupper);
    if(XArrayName == "THETA" && YArrayName=="PHI")
    {
        m_XTitles.push_back(XArrayName);
        m_XTitles.push_back(YArrayName);
        m_YTitles.push_back(XArrayName);
        m_YTitles.push_back(YArrayName);
    }
    else
    {
        m_XTitles.push_back(XArrayName);
        m_YTitles.push_back(YArrayName);
    }
}


}
